﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Yahoo.Yui.Compressor;

namespace Interfleet.SE.UICompiler.Core.YahooJavaScriptCompressor
{
    /// <summary>
    /// A stream compiler agent for Yahoo UI JavaScript Compressor.
    /// </summary>
    public class YJStreamCompilerAgent : StreamCompilerAgent
    {
        /// <summary>
        /// Reads input from <paramref name="inputStream"/> and writes it
        /// on the <paramref name="outputStream"/>
        /// </summary>
        /// <param name="inputStream">A <see cref="Stream"/> supplying the input css data</param>
        /// <param name="outputStream">Output will be written to this <see cref="Stream"/></param>
        public override void Compile(Stream inputStream, Stream outputStream)
        {
            string input = string.Empty;
            using (var sr = new StreamReader(inputStream))
            {
                input = sr.ReadToEnd();
            }
            var compressedJS = JavaScriptCompressor.Compress(input);
            using (var sw = new StreamWriter(outputStream))
            {
                sw.Write(compressedJS);
                sw.Flush();
            }
        }
    }
}
